# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import MySQLdb
from DBUtils.PooledDB import PooledDB
from learnscrapy.items import LearnscrapyItem, X23sContentItem
#from twisted.enterprise import adbapi

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

class LearnscrapyPipeline(object):
    def __init__(self, pool):
        self.pool = pool
        self.conn = self.pool.connection()
        self.curs = self.conn.cursor()

    @classmethod
    def from_settings(cls, settings):
        dbparams = dict(
            host = settings['MYSQL_HOST'],
            db = settings['MYSQL_DBNAME'],
            user = settings['MYSQL_USER'],
            passwd = settings['MYSQL_PASSWD'],
            charset = 'utf8',
            use_unicode = False
        )
        pool = PooledDB(MySQLdb, 5, **dbparams)
        return cls(pool)

    def process_item(self, item, spider):
        if isinstance(item, LearnscrapyItem):
            self._do_insert_name(item)
        elif isinstance( item, X23sContentItem ):
            self._do_insert_content(item)

    def _do_insert_name(self, item):
        sql = "insert into book_list (b_id, name, author, date ) values (%d, '%s', '%s', '%s')" % (int(item['bid']), item['name'], item['author'], item['date'])
        self.curs.execute(sql)
        self.conn.commit()

    def _do_insert_content(self, item):
        sql = "insert into book_content (b_id, title, content, url ) values (%d, '%s', '%s', '%s')" % (int(item['bid']), item['title'], item['content'], item['url'])
        self.curs.execute(sql)
        self.conn.commit()
